﻿@{
    ViewBag.Title = "ArrayDemo";
    Layout = "~/Views/Shared/_Page.cshtml";
}

<script type="text/javascript">
    
    window.onload = function() {
        /*
            Enumerate an array
         */

        ZEUS.DEBUG.log("1. ** Enumerate an array **");
        
        var arrStar = ["Jordan", "Kobe", "James"];
        ZEUS.DEBUG.log("Great NBA Stars:");
        for (var i = 0; i < arrStar.length; i++) {
            ZEUS.DEBUG.log(arrStar[i]);        
        }

        ZEUS.DEBUG.log("<br />");

        /*
            Multiple Array
         */

        ZEUS.DEBUG.log("2. ** Multiple Array **");

        var table = new Array(10);
        for (var i = 0; i < table.length; i++) {
            table[i] = new Array(10);
        }

        for (var row = 0; row < table.length; row++) {
            for (var col = 0; col < table[row].length; col++) {
                table[row][col] = row * col;
            }
        }
        ZEUS.DEBUG.log("table[9][9]: " + table[9][9]);
        ZEUS.DEBUG.log("<br />");

        /*
            Array Join
        */

        ZEUS.DEBUG.log("3. ** Array Join **");

        var arr = [1, 2, 3];
        ZEUS.DEBUG.log("default join of [1, 2, 3]: " + arr.join());
        ZEUS.DEBUG.log("join of [1, 2, 3]: " + arr.join(" "));
        ZEUS.DEBUG.log("join of [1, 2, 3]: " + arr.join("-"));

        ZEUS.DEBUG.log("<br />");

        /*
            Array reverse
        */

        ZEUS.DEBUG.log("4. ** Array reverse **");

        var arr2 = arr.reverse();
        ZEUS.DEBUG.log("reverse of [1, 2, 3]: " + arr2.join());

        ZEUS.DEBUG.log("<br />");

        /*
            Array sort
        */

        ZEUS.DEBUG.log("5. ** Array sort **");

        var arrContact = ["lucy", "david", "mark", "Green"];
        arrContact.sort();
        ZEUS.DEBUG.log(arrContact.join(", "));

        var arrNum = [99, 23, 18, 108, 6];
        arrNum.sort(function (a, b) {
            return a - b;
        });
        ZEUS.DEBUG.log(arrNum.join(", "));

        arrNum.sort(function (a, b) {
            return b - a;
        });
        ZEUS.DEBUG.log(arrNum.join(", "));

        arrContact.sort(function (a, b) {
            var str1 = a.toLocaleLowerCase();
            var str2 = b.toLocaleLowerCase();

            if (str1 < str2) return -1;
            if (str1 > str2) return 1;
            return 0;
        });
        ZEUS.DEBUG.log(arrContact.join(", "));

        ZEUS.DEBUG.log("<br />");

        /*
            Array Concat
         */

        ZEUS.DEBUG.log("6. ** Array Concat **");

        var arrNum2 = [1, 2, 3];
        arrNum2 = arrNum2.concat(4, 5);
        arrNum2 = arrNum2.concat([6, 7]);

        ZEUS.DEBUG.log(arrNum2.join(", "));

        ZEUS.DEBUG.log("<br />");

        /*
            Array slice
         */

        ZEUS.DEBUG.log("7. ** Array slice **");

        ZEUS.DEBUG.log(arrNum2.slice(0, 3).toString());
        ZEUS.DEBUG.log(arrNum2.slice(2, 5).toString());

        ZEUS.DEBUG.log("<br />");

        /*
            forEach
         */

        ZEUS.DEBUG.log("8. ** forEach **");

        var data = [1, 2, 3, 4, 5];
        var sum = 0;
        data.forEach(function (x) {
            sum += x;
        });
        ZEUS.DEBUG.log("Sum of [1 - 5]: " + sum);

        ZEUS.DEBUG.log("<br />");

        /*
            Array map
         */

        ZEUS.DEBUG.log("9. ** Array map **");

        var mapData = data.map(function (x) {
            return x * x;
        });
        ZEUS.DEBUG.log("square map of data: " + mapData.toString());

        ZEUS.DEBUG.log("<br />");

        /*
            Array filter
         */

        ZEUS.DEBUG.log("10. ** Array filter **");

        var filterData = data.filter(function (x) {
            return x > 2;
        });
        ZEUS.DEBUG.log("filter of data above 2: " + filterData.toString());

        var oddData = data.filter(function (x, i) {
            return i % 2 == 0;
        });
        ZEUS.DEBUG.log("filter of odd data: " + oddData.toString());

        ZEUS.DEBUG.log("<br />");

        /*
            every and some
         */

        ZEUS.DEBUG.log("11. ** every and some **");

        ZEUS.DEBUG.log("data: " + data.toString());
        var flag;
        flag = data.every(function(x) {
            return x < 10;
        });
        ZEUS.DEBUG.log("Every Data below 10? " + flag);

        flag = data.every(function (x) {
            return x > 3;
        });
        ZEUS.DEBUG.log("Every Data above 3? " + flag);
        
        flag = data.some(function (x) {
            return x > 3;
        });
        ZEUS.DEBUG.log("Some Data above 3? " + flag);

        ZEUS.DEBUG.log("<br />");

        /*
            indexOf
         */

        ZEUS.DEBUG.log("12. ** indexOf **");

        var arrDup = [1, 5, 8, 2, 9, 8, 99, 8, 100];

        ZEUS.DEBUG.log("Duplicated Array: " + arrDup.toString());
        ZEUS.DEBUG.log("Index of 8 in array: " + arrDup.indexOf(8));
        ZEUS.DEBUG.log("Last index of 8 in array: " + arrDup.lastIndexOf(8));

        var matchIndex = ArrayUtil.findMatchIndexes(arrDup, 8);
        ZEUS.DEBUG.log("Match index of 8 in array: " + matchIndex);

        ZEUS.DEBUG.log("<br />");
    }

</script>


<h2>Array Demo</h2>
<br />
<h3>Path: @ViewBag.Path</h3>
<br />
<h3>Extended JS: /JsLib/G_Core.js</h3>
<br />
<h3>See the result on console</h3>
<br />

